﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using System.Data;

namespace GIFT_SHOP_MVC.Controllers
{
    public class CartController : Controller
    {
        //
        // GET: /Cart/

        Models.GiftShop_LinQDataContext data = new Models.GiftShop_LinQDataContext();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AddCart(int id)
        {
            if (Session["Cart"] == null)
            {
                DataTable cart = new DataTable();
                cart.Columns.Add("Id");
                cart.Columns.Add("Count");

                DataRow product = cart.NewRow();
                product["Id"] = id;
                product["Count"] = 1;

                cart.Rows.Add(product);
                Session["Cart"] = cart;
            }
            else
            {
                DataTable cart = (DataTable)Session["Cart"];
                int flag = 1;
                foreach (DataRow product in cart.Rows)
                {
                    if (int.Parse(product["Id"].ToString()) == id)
                    {
                        product["Count"] = int.Parse(product["Count"].ToString()) + 1;
                        flag = 0;
                    }
                }
                if (flag == 1)
                {
                    DataRow product1 = cart.NewRow();
                    product1["Id"] = id;
                    product1["Count"] = 1;

                    cart.Rows.Add(product1);
                    Session["Cart"] = cart;
                }
            }
            return Redirect(Request.UrlReferrer.ToString());
        }

        public ActionResult DeleteCart(int id)
        {
            if (id == 0)
            {
                Session["Cart"] = null;
            }
            else
            {
                DataTable cart = (DataTable)Session["Cart"];
                for (int i = 0; i <= cart.Rows.Count - 1; i++)
                {
                    DataRow product = cart.Rows[i];
                    if (int.Parse(product["Id"].ToString()) == id)
                        cart.Rows[i].Delete();
                }

                if (cart.Rows.Count == 0)
                    Session["Cart"] = null;
                else
                    Session["Cart"] = cart;
            }
            return RedirectToAction("Index");
        }

        public ActionResult Payment()
        {
            if (Session["user"] == null)
                return RedirectToAction("Index", "Cart");

            DataTable cart = (DataTable)Session["Cart"];
            long price = 0;
            foreach (DataRow row in cart.Rows)
            {
                Models.SanPham product = data.GetProductById(int.Parse(row["Id"].ToString()));
                price += decimal.ToInt32(product.Gia) * int.Parse(row["Count"].ToString());
            }
            Models.User user = (Models.User)Session["user"];
            Models.PhieuXuat bill = new Models.PhieuXuat();
            bill.MaKH = user.MaUser;
            bill.TongTien = price;
            bill.NgayDatHang = DateTime.Now;
            bill.NgayXuat = new DateTime(2001, 1, 1);
            bill.Ma_TT = 1;

            data.PhieuXuats.InsertOnSubmit(bill);
            data.SubmitChanges();

            foreach (DataRow row in cart.Rows)
            {
                Models.SanPham product = data.GetProductById(int.Parse(row["Id"].ToString()));
                Models.CT_PhieuXuat DetailsBill = new Models.CT_PhieuXuat();
                price = decimal.ToInt32(product.Gia) * int.Parse(row["Count"].ToString());

                DetailsBill.MaPhieuXuat = bill.MaPhieuXuat;
                DetailsBill.MaSanPham = product.MaSanPham;
                DetailsBill.TongTien = price;
                DetailsBill.SoLuong = int.Parse(row["Count"].ToString());

                data.CT_PhieuXuats.InsertOnSubmit(DetailsBill);

                product.SL_Ton = product.SL_Ton - int.Parse(row["Count"].ToString());
                if (product.SL_Ton < 0)
                    product.Ma_TT = 2;
                product.SL_Ban = product.SL_Ban + int.Parse(row["Count"].ToString());

                data.SubmitChanges();
            }

            Session["Cart"] = null;
            return RedirectToAction("Index", "Home");
        }

        public ActionResult SaveChanges(FormCollection newTable)
        {
            Session["OverAmount"] = "";
            DataTable dtCart = (DataTable)Session["Cart"];
            foreach (DataRow item in dtCart.Rows)
            {
                //kiem tra sl thay doi co vuot qua slTon
                if (int.Parse(newTable.GetValue(item["ID"].ToString()).AttemptedValue) > data.SanPhams.Single(p => p.MaSanPham == int.Parse(item["ID"].ToString())).SL_Ton)
                {
                    string a = Session["OverAmount"].ToString();
                    a += item["ID"].ToString() + ",";
                    Session["OverAmount"] = a;
                    continue;
                }
                //lay gia tri cua cac textbox co ID trung voi ID san pham(Xem lai phan tao form trong Index.aspx)
                item["Count"] = int.Parse(newTable.GetValue(item["ID"].ToString()).AttemptedValue);

            }
            //xoa dau phay cuoi cung
            if (Session["OverAmount"].ToString() != "")
            {
                string c = Session["OverAmount"].ToString();
                c = c.TrimEnd(',');
                Session["OverAmount"] = c;
            }

            return RedirectToAction("Index");
        }
    }
}
